﻿using Server.CustomObjects;
using Server.DataBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Server
{
    public partial class Form1 : Form
    {
        ServerCore cServerCore;

        public Form1()
        {
            this.cServerCore = new ServerCore();
            Log.NewCustomLog += Log_NewCustomLog;
            Log.NewMessage += Log_NewMessage;
            InitializeComponent();

        }

        void Log_NewMessage(string pTimeStamp, string pDirection, string pMessageType, string pSourceID, string pTargetID, string pContent)
        {
            this.Invoke((MethodInvoker)delegate
            {
                this.cDGVMessages.Rows.Add(pTimeStamp, pDirection, pMessageType, pSourceID, pTargetID, pContent);
            });
        }

        void Log_NewCustomLog(string pTimeStamp, string pType, string pContent)
        {
            this.Invoke((MethodInvoker)delegate
            {
                this.cDGVCustomLogs.Rows.Add(pTimeStamp, pType, pContent);
            });
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            this.tbCpu.Text = this.cServerCore.CpuUsage.ToString() + "%";
            this.tbRam.Text = this.cServerCore.AvailableMemory.ToString() + "Mb";
            this.tbUser.Text = this.cServerCore.UserCounter.ToString();
            this.tbSessions.Text = this.cServerCore.SessionCounter.ToString();
            this.tbCacheCount.Text = "" +this.cServerCore.CacheCounter;
        }

        private void cbCacheActive_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox _cb = (System.Windows.Forms.CheckBox)(sender);
            if (_cb.Checked) cServerCore.StartResendCache();
            else cServerCore.StopResendCache();
        }


    }
}
